import { createStore, storeKey } from "vuex";

export default createStore({
    state: {
        cartlist: [],//购物车的列表
        orderlist: [],//生成订单的列表
        orderlistcontact: [],//生成订单的联系人
        orderlisted: [],//生成订单之后的列表
        edit: true,
        userAddress: [
            {
                addressDetail: "zxc",
                areaCode: "110102",
                city: "广州市",
                county: "海珠区",
                id: 1001,
                country: "",
                isDefault: false,
                name: "张三",
                postalCode: "",
                province: "广东省",
                tel: "13555666444",

            },
            {
                addressDetail: "zxc",
                areaCode: "120202",
                city: "宁波市",
                country: "",
                county: "下城区",
                id: 1002,
                isDefault: true,
                name: "张四",
                postalCode: "",
                province: "浙江省",
                tel: "13555666444",
            },
        ]
    },
    mutations: {
        //新增购物车
        ADDCART(state, value) {
            state.cartlist = value
        },
        //结算订单
        PAY(state, value) {
            state.orderlist = value
        },
        //删除
        DELETE(state, value) {
            state.cartlist = value
        },
        //生成订单
        UPDATEORDER(state) {
            state.orderlisted = state.orderlisted.concat(state.orderlist)
        },
        //订单联系人
        CONTACT(state, value) {
            if (value === "默认") {
                state.orderlistcontact = []
            }
            else {
                state.orderlistcontact.push(value)
            }
        },
        //添加地址
        ADDADDRESS(state, value) {
            let id = state.userAddress[state.userAddress.length - 1].id + 1
            state.userAddress.map((item) => {
                if (value.isDefault) {
                    item.isDefault = false
                }
            })
            value.id = id
            state.userAddress.push(value)
        },
        //修改地址
        CHANGEADDRESS(state, value) {
            state.userAddress = state.userAddress.map((item) => {
                if (value.isDefault) {
                    item.isDefault = false
                }
                return item.id === value.id ? value : item
            })
        },
        //删除地址
        DELETEADDRESS(state, value) {
            state.userAddress = state.userAddress.filter((item) => {
                return !(item.id === value.id)
            });
            if (value.isDefault && state.userAddress.length) {
                state.userAddress[0].isDefault = true
            }
        },
        //编辑的按钮
        EDIT(state, value) {
            if (value === "delete") {
                state.edit = true
            }
            else {
                state.edit = !state.edit
            }
        }
    },
    actions: {},
    getters: {},
})